package redsig.signature;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import core.Adapter;



/**
 * @author  Bacher
 */
public class KeyGenerator {
	
	/**
	 * @uml.property  name="instance"
	 * @uml.associationEnd  
	 */
	private static KeyGenerator instance = null;
	private PrivateKey privateKey = null;
	private PublicKey publicKey = null;
	
	private KeyGenerator(){
		try {
			KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
			keyGen.initialize(1024);
			
			KeyPair keyPair = keyGen.generateKeyPair();
			privateKey = keyPair.getPrivate();
			publicKey = keyPair.getPublic();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	/**
	 * @return
	 * @uml.property  name="instance"
	 */
	public static KeyGenerator getInstance(){
		if(instance == null){
			instance = new KeyGenerator();
		}
		return instance;
	}
	
	/**
	 * @return
	 * @uml.property  name="privateKey"
	 */
	public PrivateKey getPrivateKey(){
		return privateKey;
	}

	/**
	 * @return
	 * @uml.property  name="publicKey"
	 */
	public PublicKey getPublicKey(){
		return publicKey;
	}
	
	public static void main(String[] args){
		KeyGenerator keyGen = KeyGenerator.getInstance();
		
		System.out.println("Private Key: " + Adapter.byteToBase64(keyGen.getPrivateKey().getEncoded()));
		System.out.println("Public Key: " + Adapter.byteToBase64(keyGen.getPublicKey().getEncoded()));
		
		System.out.println("Private Format: "+keyGen.getPrivateKey().getFormat());
		System.out.println("Public Format: "+keyGen.getPublicKey().getFormat());
	}
}
